package com.xiaomi.back.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.back.service.user.UserBackService;
import com.xiaomi.pojo.User;
import com.xiaomi.util.Todaytime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserbackController {

    @Autowired
    private UserBackService userBackService;

    //查看今日登陆用户
    @RequestMapping("/LoginUser")
    public String TodayUser(@RequestParam(defaultValue = "1") int currentPageNo, Model model) {

        //设置当前页码和几条数据
        PageHelper.startPage(currentPageNo, 10);
        //获取今日登陆用户集合
        List<User> userList = userBackService.LoginCount(Todaytime.Todaytime());
        PageInfo<User> page = new PageInfo<>(userList);

        model.addAttribute("page", page);

        //获取今日登陆用户数量
        int count = userList.size();
        model.addAttribute("count", count);
        return "back/User/Users_today";
    }

    //查询正常全部
    @RequestMapping("/allUser")
    public String userlist(Model model, @RequestParam(defaultValue = "1") int currentPageNo) {

        //设置当前页码和几条数据
        PageHelper.startPage(currentPageNo, 10);
        List<User> users = userBackService.getUserlist();
        PageInfo<User> page = new PageInfo<User>(users);

        model.addAttribute("page", page);
        return "back/User/userlist";
    }

    //查询冻结全部
    @RequestMapping("/allUnseal")
    public String allUnseal(Model model, @RequestParam(defaultValue = "1") int currentPageNo) {

        //设置当前页码和几条数据
        PageHelper.startPage(currentPageNo, 10);
        List<User> users = userBackService.getUnseal();
        PageInfo<User> page = new PageInfo<User>(users);

        model.addAttribute("page", page);
        return "back/User/Unseal";
    }

    //按照手机号码模糊查询
    @PostMapping("/findUser")
    public String findUser(@RequestParam("pnumber") String phone, @RequestParam(defaultValue = "1") int currentPageNo, Model model) {

        PageHelper.startPage(currentPageNo,10);
        List<User> users = userBackService.getAllUser(phone);
        PageInfo<User> page=new PageInfo<>(users);
        model.addAttribute("page", page);
        return "back/User/userFind";
    }


    //解封 冻结 用户
    @RequestMapping("/Feozen")
    public String Feozen(int uid,int state,Model model){
        int result=userBackService.Updatefrozen(uid,state);
        model.addAttribute("result",result);
        return "back/User/UserResult";
    }

}
